import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ListIterator;
import java.util.LinkedList;

public class Bashpage {
	public static final String BASH_PAGE_URL="http://www.bash.org/?browse&p=";
	private int pagenumber;
	private LinkedList<String> page;
	private LinkedList<Bashquote> quotes;

	public Bashpage(int pagenumber) {
		this.pagenumber=pagenumber;
		this.page=new LinkedList<String>();
		this.quotes=new LinkedList<Bashquote>();
		this.getPage();
	}

	@SuppressWarnings("static-access")
	public LinkedList<String> getPage () {
		if (this.page.size()!=0)
			return page;

		try {
			URLConnection con = new URL(this.BASH_PAGE_URL + ((this.pagenumber < 10) ? "0" + this.pagenumber:this.pagenumber) ).openConnection();
	        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
	        String read;
	        while((read=br.readLine())!=null) {
	           page.add(read);
	        } 
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		return this.page;
	}
	
	public String getPageString() {
		String retval=new String();
		Iterator<String> it;
		
		it=this.page.listIterator();
		while(it.hasNext()) {
			retval+=it.next();
		}
		return retval;
	}
	
	public int getMaxPageNum () {
		if(null==this.page||this.page.size()==0) {
			this.getPage();
		}

		String s="";

		Iterator<String> it=this.page.listIterator();
		while(it.hasNext()) {
			s=it.next();
			if(s.endsWith("</option></form></center></font></td></tr></table></center><center>")) {
				s=s.substring(s.length() - "</option></form></center></font></td></tr></table></center><center>".length() - 3, s.length());
				s=s.substring(0, 3);
				break;
			}
		};
		return Integer.parseInt(s);
	}

//	<p class="quote"><a href="?15" title="Permanent link to this quote."><b>#15</b></a> <a href="./?le=57ab5991c154679192a7cfa216d1c5de&amp;rox=15" class="qa">+</a>(2620)<a href="./?le=57ab5991c154679192a7cfa216d1c5de&amp;sox=15" class="qa">-</a> <a href="./?le=57ab5991c154679192a7cfa216d1c5de&amp;sux=15" onClick="return confirm('Flag quote for review?');" class="qa">[X]</a></p><p class="qt">&lt;emufreak&gt; I HATE CHANNEL TOPICS<br />
//	&lt;emufreak&gt; WHY MUST THEY EXIST<br />
//	&lt;emufreak&gt; AND WHY DO I STILL CLICK THEM WHEN THEY END IN .JPG</p>

	public LinkedList<Bashquote> getQuotes() {
		String s="";
		boolean inquote=false;
		int currentquote=0;
		ListIterator<String> it=this.page.listIterator();
		LinkedList<String> tempquote=new LinkedList<String>();
		while(it.hasNext()) {
			s=it.next();
			if(inquote) {
				
			} else {
				if(s.startsWith("<p class=\"quote\"")) {
					s=s.substring("<p class=\"quote\"><a href=\"?".length());
					if(tempquote.size()>0) {
						Bashquote bq = new Bashquote();
					}
					tempquote.clear();
					tempquote.add(s);
					inquote=true;
				} else if (s.startsWith("</td>")){
					//Quotebehandlung
					inquote=false;
				}
			}
			if(s.startsWith("<p class=\"quote\"")) {//Quotes fangen an
				tempquote.clear();
				tempquote.add(s);
				s="";
				while((!s.startsWith("<p class=\"quote\""))&&(!(s.startsWith("</td>")))) {
					tempquote.add(s);
				}

				//Bearbeitung der ausgeschnittenen Zeilen
			}
		}
		return this.quotes;
	}
}
